package com.cash.loan.modular.cash.service.impl;

import com.cash.loan.modular.cash.dao.LoanRecommendMapper;
import com.cash.loan.modular.cash.model.LoanRecommend;
import com.cash.loan.modular.cash.service.RecommendService;
import com.cash.loan.modular.cash.transfer.ListRecommendVo;
import com.cash.loan.modular.cash.transfer.RecommendDto;
import com.cash.loan.modular.cash.transfer.updateRecommendDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @Date 2018-09-30 14:53
 */
@Service
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private LoanRecommendMapper recommendMapper;

    @Override
    public List<ListRecommendVo> list() {
        List<ListRecommendVo> listRecommendVoList = recommendMapper.listRecommendAndProduct();
        return listRecommendVoList;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean add(RecommendDto recommend) {
        LoanRecommend loanRecommend = new LoanRecommend();
        BeanUtils.copyProperties(recommend,loanRecommend);
        loanRecommend.setProductId(Long.valueOf(recommend.getProductId()));
        Integer insertRes = recommendMapper.insert(loanRecommend);
        if(insertRes != 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean update(updateRecommendDto recommend) {
        LoanRecommend loanRecommend = new LoanRecommend();
        BeanUtils.copyProperties(recommend,loanRecommend);
        loanRecommend.setProductId(Long.valueOf(recommend.getProductId()));
        Integer updateRes = recommendMapper.updateAllColumnById(loanRecommend);
        if(updateRes != 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public LoanRecommend detail(Integer id) {
        return recommendMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean delete(Integer id) {
        Integer delRes = recommendMapper.deleteById(id);
        if(delRes != 0) {
            return true;
        } else {
            return false;
        }
    }
}
